package top.lshaci.learning.springboot.file.controller;

import cn.hutool.core.io.IoUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileOutputStream;
import java.io.IOException;

/**
 * FileController
 *
 * @author liangxu
 */
@RestController
@RequestMapping("/file")
public class FileController {

    @PostMapping("/upload")
    public ResponseEntity<Object> upload(MultipartFile file) throws IOException {
        String path = System.getProperty("user.dir") + "/spring-boot/spring-boot-file";
        String originalFilename = file.getOriginalFilename();
        String destFileName = path + "/" + originalFilename;
        IoUtil.copy(file.getInputStream(), new FileOutputStream(destFileName));
        return new ResponseEntity<>(destFileName, HttpStatus.OK);
    }

}
